package com.lanyanhua.job.controller;

import com.lanyanhua.job.entity.JobGroup;
import com.lanyanhua.job.entity.LayerResult;
import com.lanyanhua.job.entity.Result;
import com.lanyanhua.job.service.IJobGroupService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.exception.ExceptionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;


/**
 * @Auther: lanyanhua
 * @Date: 2018/10/18 0018 16:56
 * @Description:
 */
@Slf4j
@RestController
@RequestMapping("/jobGroup")
public class JobGroupController {

    private IJobGroupService groupService;

    @Autowired
    public JobGroupController(IJobGroupService groupService) {
        this.groupService = groupService;
    }

    /**
     * 查询所有分组信息
     */
    @GetMapping("/getGroupAll")
    public Object getGroupAll(){
        return groupService.findAll();
    }

    @PostMapping("/saveJobGroup")
    public Result saveJobGroup(JobGroup jobGroup){
        try {
            groupService.save(jobGroup);
            return new Result(true,"保存成功!");
        }catch (Exception e){
            log.error("保存邮箱数据报错："+ ExceptionUtils.getStackTrace(e));
            return new Result(false,"保存失败："+e.getMessage());
        }
    }

    @GetMapping(value="/findJobGroupPage")
    public Object findJobGroupPage(JobGroup jobGroup, Integer page, Integer limit)
    {
        return groupService.findJobGroupPage(jobGroup,page-1,limit);
    }

    @GetMapping(value="/findJobGroupAll")
    public Object findJobGroupAll()
    {
        return groupService.findAll();
    }

    @GetMapping("/existsById")
    public LayerResult existsById(String target){
        if(!groupService.existsById(target)){
            return new LayerResult(true);
        }else{
            return new LayerResult("该项目编号已被使用！！！");
        }
    }
}
